package com.example.ivory.accountbook.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.ivory.accountbook.R;
import com.example.ivory.accountbook.packetActivity.InPacketsActivity;
import com.example.ivory.accountbook.packetActivity.ShowAdapter;
import com.example.ivory.accountbook.packetActivity.addpacketActivity;
import com.example.ivory.accountbook.packetActivity.inpacketActivity;
import com.example.ivory.accountbook.uilt.TittleManager;

import java.util.*;

import de.greenrobot.event.EventBus;

/**
 * Created by 王如梦 on 2016/8/26.
 */
public class Fragment_wallet extends basefragment implements View.OnClickListener {
    //绑定钱包主界面的ListView
    private ListView listviewShowPackets;
    private ShowAdapter adapterShowPacket;

    //用来记录所有钱包的数据
    private static List<Map<String,Object>> listShowPacket=new ArrayList<Map<String,Object>>();
    private Map<String,Object> map;
    private static int flag=0;

    //静态添加
    static {
        //添加listView中的内容
        Map<String,Object> tempmap=new HashMap<String,Object>();
        //设置最开始的一行item
        tempmap.put("name","余额");
        tempmap.put("type","notype");
        tempmap.put("money","0");
        tempmap.put("logo","nologo");
        tempmap.put("color",1);
        listShowPacket.add(tempmap);

        //设置最后一行item
        tempmap=new HashMap<String,Object>();
        tempmap.put("name","添加账户");
        tempmap.put("type","notype");
        tempmap.put("money","nomoney");
        tempmap.put("logo", "logo");
        tempmap.put("color", 3);
        listShowPacket.add(tempmap);
    }

    private View mContent;
    private TittleManager tittleManager;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContent = inflater.inflate(R.layout.fragment_wallet, container, false);


        tittleManager=new TittleManager(mContent);
        tittleManager.setTitleStyle(TittleManager.TitleStyle.ONLY_TITLE,"钱包");
        //绑定界面的展示钱包的listview
        listviewShowPackets= (ListView) mContent.findViewById(R.id.listviewShowPacket);

        return mContent;
    }

    @Override
    protected void initEvent() {

        if(flag==0){
            //注册eventbus
            EventBus.getDefault().register(this);
            flag=1;

        }
        //创建适配器和绑定
        adapterShowPacket=new ShowAdapter(mActivity,listShowPacket);
        listviewShowPackets.setAdapter(adapterShowPacket);


        //设置钱包主界面listView的每个item的监听事件
        listviewShowPackets.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 获取每一行上的信息，并且判断进入页面
                TextView txttype= (TextView) view.findViewById(R.id.textviewPacketType);
                TextView txtmoney= (TextView) view.findViewById(R.id.textviewPacketBalance);
                TextView txtname= (TextView) view.findViewById(R.id.textviewPacketName);

                //显示余额俩字
                String type=txttype.getText().toString();

                //根据钱包类型判断跳转到哪个界面
                Intent intent;
                if(type.equals("现金余额")||type.equals("储蓄卡余额")||type.equals("信用卡余额")||type.equals("网络账户余额")||type.equals("投资账户余额")){
                    intent=new Intent(mActivity,inpacketActivity.class);
                    intent.putExtra("name",txtname.getText().toString());
                    intent.putExtra("money",txtmoney.getText().toString());

                }else if(type.equals("应收账余额")||type.equals("应付账余额")){
                    intent=new Intent(mActivity, InPacketsActivity.class);
                }else{
                    intent=new Intent(mActivity,addpacketActivity.class);
                }

                startActivity(intent);

            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    public void onEventMainThread(Map<String,Object> temp){
        DealPacket(temp);
    }

    public void DealPacket(Map<String,Object> temp){
        map=new HashMap<String,Object>();

        map.put("name",temp.get("name"));
        map.put("type",temp.get("type"));
        map.put("money",temp.get("money"));
        map.put("logo", "logo");
        map.put("color", 3);
        this.listShowPacket.add(listShowPacket.size()-1,map);

        System.out.println(listShowPacket);
        //创建适配器和绑定
        adapterShowPacket=new ShowAdapter(mActivity,listShowPacket);
        listviewShowPackets.setAdapter(adapterShowPacket);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        EventBus.getDefault().unregister(this);//反注册EventBus
        flag=0;
    }
}
